package com.five.admin.service.impl;

import com.five.admin.service.CarouselService;
import com.five.admin.service.StatisticsService;
import com.five.admin.dao.StatisticsMapper;
import com.five.entity.qo.CourseSalesVolume;
import com.five.entity.qo.SalesAmount;
import com.five.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:40
 * 统计 Service层 实现类
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    /**
     * 计算当天收入
     * @return
     */
    @Override
    public BigDecimal countRevenueByToday() {
        return statisticsMapper.countRevenueByToday();
    }

    /**
     * 计算用户数
     * @return
     */
    @Override
    public int countUserNumber() {
        return statisticsMapper.countUserNumber();
    }

    /**
     * 计算课程数
     * @return
     */
    @Override
    public int countCourseNumber() {
        return statisticsMapper.countCourseNumber();
    }

    /**
     * 计算视频播放数
     * @return
     */
    @Override
    public int countChapterView() {
        return statisticsMapper.countChapterView();
    }

    /**
     * 统计销量前五的课程
     * @return
     */
    @Override
    public List<CourseSalesVolume> countCourseSalesVolumeTOP5() {
        return statisticsMapper.countCourseSalesVolumeTOP5();
    }

    @Override
    public List<SalesAmount> countSalesAmountBeforeSevenDay() {
        List<SalesAmount> salesAmounts = statisticsMapper.countSalesAmountBeforeSevenDay(StringUtil.getBeforeDaysDate(new Date(), 7));
        ArrayList<String> beforeDaysList = StringUtil.getBeforeDaysList(7);
        List<SalesAmount> allSalesAmounts = new ArrayList<SalesAmount>();
        int i = 0;
        for(String dateStr:beforeDaysList){
            int flag = i;
            while (i < salesAmounts.size()){
                if(salesAmounts.get(i).getSalesDate().equals(dateStr)){
                    allSalesAmounts.add(salesAmounts.get(i));
                    i++;
                    break;
                }
                break;
            }
            if(flag == i){
                SalesAmount salesAmount = new SalesAmount();
                salesAmount.setSalesDate(dateStr);
                allSalesAmounts.add(salesAmount);
            }
        }
        return allSalesAmounts;
    }


}
